package com.ujoku.service.impl;

import com.labillusion.core.database.base.mybatis.dao.BaseDao;
import com.labillusion.core.database.base.mybatis.service.base.impl.BaseServiceImpl;
import com.labillusion.core.platform.iOSPush.IOSMessagePushService;
import com.labillusion.core.util.StringUtils;
import com.labillusion.core.util.UUIDUtils;
import com.ujoku.context.constants.ExpiredCacheContext;
import com.ujoku.context.constants.MessageConstants;
import com.ujoku.dao.MessageDao;
import com.ujoku.domain.Member;
import com.ujoku.domain.Message;
import com.ujoku.service.MemberService;
import com.ujoku.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 14-10-25.
 */
@Service
public class MessageServiceImpl extends BaseServiceImpl<Message> implements MessageService<Message> {

    @Autowired
    private MessageDao dao;

    @Autowired
    private MemberService memberService;

/*    @Autowired
    private IOSMessagePushThread iosMessagePushThread;*/

    @Autowired
    private IOSMessagePushService iOSMessagePushService;

    @Override
    protected BaseDao<Message> getBaseDao() {
        return this.dao;
    }

    private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);

    @Override
    public Message bulid(String type, int toMemberId, String[] value){
        Message message = new Message();

        message.setId(UUIDUtils.random());
        message.setType(type);

        String content = MessageConstants.getMessage(type, value);
        String sender = MessageConstants.getSender(type);
        if(value != null)
            message.setActionValue(value.length > 0? value[0]: null);
        message.setContent(content);
        message.setToMemberId(toMemberId);

        Member member = (Member)memberService.selectById(toMemberId);

       //MessageServiceImpl.class.getProtectionDomain().getCodeSource().getLocation().getPath()

        /*if(StringUtils.hasText(member.getiOSDeviceToken())) {
            IOSMessagePushThread thread = iosMessagePushThread;
            thread.setiOSDeviceToken(member.getiOSDeviceToken());
            thread.setContent(content);

            fixedThreadPool.execute(thread);
        }*/

        if(StringUtils.hasText(member.getiOSDeviceToken())) {
            iOSMessagePushService.run(member.getiOSDeviceToken(), content, sender,true);
        }

        return message;
    }

    @Override
    public int updateByToMemberId(int toMemberId){

        return dao.updateByToMemberId(toMemberId);
    }

    @Override
    public int insert(Message message){
        ExpiredCacheContext.expiredMemberSet.add(message.getToMemberId());

        return dao.insert(message);
    }

    @Override
    public int updateOne(Map<String,Object> query){
        return dao.updateOne(query);
    }

}
